package com.ingenio.siot.adapter;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.ingenio.siot.Adapter;
import com.ingenio.siot.entity.SentidoCalle;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

public class SentidoCalleAdapter extends Adapter{
	
	public List<SentidoCalle> devolverTodos() throws Exception{
    	Connection conn = null;
    	List<SentidoCalle> sentidoscalles = new ArrayList<SentidoCalle>();
    	PreparedStatement stmt = null;
		try {
			conn = (Connection) abrirConexion();
			String sel ="SELECT * from sentidoscalles";
    		stmt = (PreparedStatement) conn.prepareStatement(sel);
    		ResultSet result = stmt.executeQuery();
    		SentidoCalle sc = null;
    		
    		while(result.next()){
    			Integer id = result.getInt("IdSentido");
    			String desc = result.getString("descSentido");
    			sc = new SentidoCalle(id, desc); 
    			sentidoscalles.add(sc);
    		}
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new Exception("Error al conectar con la base de datos.", e);
		}
    	finally{
    		//conn.close();
    		cerrarConexion(conn);
    	}
    	return sentidoscalles;
    }
	
	public SentidoCalle devolverSentidoCalle(Integer idSentidoCalle) throws Exception{
		Connection conn = null;
		SentidoCalle sc = null;
		PreparedStatement stmt = null;
		try{
			conn = (Connection) abrirConexion();
			String query = "SELECT * FROM sentidoscalles WHERE IdSentido = ?";
			stmt = (PreparedStatement) conn.prepareStatement(query);
			stmt.setInt(1, idSentidoCalle);
			ResultSet result = stmt.executeQuery();
			if(result.next()){
				Integer id = result.getInt("IdSentido");
				String desc = result.getString("descSentido");
				sc = new SentidoCalle(id, desc); 
			}
		}
		catch (Exception e){
			e.printStackTrace();
			throw new Exception("Error al conectar con la base de datos", e);
		}
		finally{
			cerrarConexion(conn);
		}
		return sc;
	}
	
}
